package com.example.tests;

import java.util.Random;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class IssueCreate extends BaseTestSetUp {

	@Test (dataProvider = "issueDataProvider")
	public void issueCreate(IssueCreateParameter parameters) throws Exception {
		app.navigateTo().projectsPageByLink();
		app.driver.findElement(By.linkText("login_project_1")).click();
		createIssue(parameters);
		
		//createIssue(new IssueCreateParameter("subject", "Feature", "this is bug description", "<< me >>", "8"));
		//String Bug #13: subject - login_project_1 - Redmine demo = driver.getTitle();
	}

	
	private IssueCreateParameter generateRandomIssueData(){
		Random rnd= new Random();
		Integer num=rnd.nextInt(9999);
		return new IssueCreateParameter("Bug", "issueSubject"+num, "issueDescription"+num, "<< me >>", num.toString());
	}

	protected void createIssue(IssueCreateParameter parameterObject) {
		
		app.driver.findElement(By.cssSelector("html body.controller-projects div#wrapper div#wrapper2 div#wrapper3 div#header div#main-menu ul li a.new-issue")).click();
		new Select(app.driver.findElement(By.id("issue_tracker_id"))).selectByVisibleText(parameterObject.issueType);
		app.driver.findElement(By.id("issue_subject")).sendKeys(parameterObject.issueSubject);
		app.driver.findElement(By.id("issue_description")).clear();
		app.driver.findElement(By.id("issue_description")).sendKeys(parameterObject.issueDescription);
		new Select(app.driver.findElement(By.id("issue_assigned_to_id"))).selectByVisibleText(parameterObject.issueAssignee);
		app.driver.findElement(By.cssSelector("img.ui-datepicker-trigger")).click();
		app.driver.findElement(By.linkText("17")).click();
		app.driver.findElement(By.id("issue_estimated_hours")).sendKeys(parameterObject.issueEstimHours);
		app.driver.findElement(By.name("commit")).click();
	}
	
	@DataProvider (name="issueDataProvider")
	public Object[][] issueDataProviderMethod(){
		Object[][] issueDataArray = new Object[3][];
		for (int i = 0; i < issueDataArray.length; i++) {
			issueDataArray[i]= new Object[]{generateRandomIssueData()};
		}
		return issueDataArray; 
		
	}
	

}
